package com.markhsiu.store;

import java.io.File;
import java.util.concurrent.atomic.AtomicBoolean;

import com.markhsiu.store.disk.StoreTopicService;

import io.openmessaging.KeyValue;
import io.openmessaging.demo.DefaultBytesMessage;

public class StoreDB {
 
	public  static final int MAX_BYTE = 4 * 1024;
	public static final ThreadLocal<DefaultBytesMessage> messagePool = new ThreadLocal<>();
 	
	public static final String SEP = File.separator;
	public static  String ROOT_PATH;
	public static final byte MAGIC = 0x23;//#
	public static boolean isProducer ;
	private static final AtomicBoolean flag = new AtomicBoolean(false);
	public static  final StoreTopicService service = new StoreTopicService();
 
	private StoreDB(){}
	
	public static void builder(KeyValue properties, boolean isPro){
		if(flag.compareAndSet(false, true)){
			ROOT_PATH = properties.getString("STORE_PATH")+ SEP;
			isProducer = isPro;
			service.init();
		}
	} 
	
 public static byte[] newBytes(){
	 return new byte[MAX_BYTE];
 }
	
	public static void send(DefaultBytesMessage message){
		service.put(message);
	}
	
	public static DefaultBytesMessage pull(String topic){
		return service.poll(topic);
	}
	
	public static void flush(){
		service.flush();
	}
	
	public static String getRootDir(){
		return ROOT_PATH ;
	}
	
	
}
